package statistics;

import java.util.Random;

/**
 * This class represents a Geometric Distribution.
 */

public class GeometricDistribution extends DiscreteDistribution {

  private double p;

  /**
   * Constructs a Geometric Distribution with parameter p.
   * @param p the parameter of the Geometric Distribution with expectation 1/p.
   */

  public GeometricDistribution(double p) {
    this.p = p;
    random = new Random();
  }

  /**
   * Constructs a Geometric Distribution with parameter p. You must also specify the
   * random seed that will be used to realize random variables with this distribution.
   * @param p the parameter of the Geometric Distribution with expectation 1/p.
   * @param random the random seed.
   */

  public GeometricDistribution(double p, Random random) {
    this.p = p;
    this.random = random;
  }

  /**
   * Returns the expectation of a Geometric distribution.
   */

  public double expectation() {
    return p/(1-p);
  }

  /**
   * Returns the variance of a Geometric distribution.
   */

  public double variance() {
    return p/((1-p)*(1-p));
  }

  /**
   * Returns P(X = k), X is a random variable with a Geometric distribution,k = 0,1,2....
   * @param k
   */

  public double probabilityFunction(int k) {
    if (k <= 0) return 0;
      else return (1-p)*Math.pow(p,k-1);
  }

  /**
   * Returns a realization of a random variable with a Geometric distribution.
   */

  public double nextRandom() {
    double u = random.nextDouble();
    return Math.floor(Math.log(u)/Math.log(p));
  }
}
